package com.demo.microblockchain;

import java.security.PublicKey;

import com.demo.microblockchain.utils.StringUtil;

/**
 * 交易输出类将显示从交易中发送给每一方的最终金额。这些作为新交易中的输入参考，作为证明你可以发送的金额数量。
 *
 * @author zlf
 * @email i@merryyou.cn
 * @since 1.0
 */
public class TransactionOutput {

    public String id;
    public PublicKey reciepient; //also known as the new owner of these coins.
    public float value; //the amount of coins they own
    public String parentTransactionId; //the id of the transaction this output was created in

    //Constructor
    public TransactionOutput(PublicKey reciepient, float value, String parentTransactionId) {
        this.reciepient = reciepient;
        this.value = value;
        this.parentTransactionId = parentTransactionId;
        this.id = StringUtil.applySha256(StringUtil.getStringFromKey(reciepient)+Float.toString(value)+parentTransactionId);
    }

    //Check if coin belongs to you
    public boolean isMine(PublicKey publicKey) {
        return (publicKey == reciepient);
    }

	@Override
	public String toString() {
		return "TransactionOutput [id=" + id + ", reciepient=" + reciepient.getClass().getSimpleName()
				+ ", value=" + value + ", parentTransactionId="
				+ parentTransactionId + "]";
	}

}